package thread.threadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * aims:
 * 1.示例Pool Class
 * 2.定时调度任务的方法
 * Created by E440 on 2017/4/21.
 */
public class ThreadPoolTest {
    public static void main(String[] args) {

        // ExecutorService executorService= Executors.newFixedThreadPool(3);
        // ExecutorService executorService= Executors.newCachedThreadPool();
        ExecutorService executorService = Executors.newSingleThreadExecutor();    // 线程运行完成后，自动新建一个线程运行

        for (int i = 0; i < 10; i++) {
            final int id       = i;
            Runnable  runnable = new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10; j++) {
                        System.out.println(Thread.currentThread().getName() + " is running " + j
                                           + " loop of 10 in task " + "of " + id);
                    }
                }
            };

            executorService.execute(runnable);
        }

        executorService.shutdown();    // 程序执行完关闭

        // executorService.shutdownNow();//不等程序结束，强制关闭
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
